<template>
  <view>

    <!--		期待上门时间-->
    <view class="uni-title uni-common-pl">期待上门日期</view>
    <view class="uni-list">
      <view class="uni-list-cell">
        <view class="uni-list-cell-db">
          <picker mode="date" :value="zyRepair.doorTime" :start="startDate" :end="endDate" @change="bindDateChange">
            <view class="uni-input">{{ zyRepair.doorTime }}</view>
          </picker>
        </view>
      </view>
    </view>
    <!--		报修内容-->
    <view class="uni-common-mt">
      <view class="uni-form-item uni-column">
        <view class="title">报修内容</view>
        <input class="uni-input" :value="zyRepair.repairContent" @input="onKeyInput" focus placeholder="报修内容"/>
      </view>
    </view>
    <!--		小区Id-->
    <view class="uni-title uni-common-pl">小区Id</view>
    <view class="uni-list-cell-db">
      <picker mode="selector" :value="zyCommunityIndex" :range="zyCommunityList" range-key="communityName" @change="bindPickerChange">
        <view class="uni-input">{{selectedzyCommunityName}}</view>
      </picker>
    </view>
    <!--		详细地址-->
    <view class="uni-common-mt">
      <view class="uni-form-item uni-column">
        <view class="title">详细地址</view>
        <input class="uni-input" :value="zyRepair.address" @input="onAddressKeyInput" focus placeholder="详细地址"/>
      </view>
    </view>
    <uni-section :title="'日期时间用法：' + zyRepair.doorTime" type="line"></uni-section>
    <view class="example-body">
      <uni-datetime-picker type="datetime" v-model="zyRepair.doorTime" @change="changeLog" />
    </view>
    <button type="default"  plain="true" @click="commit">提交</button>
    <!--		取消时间-->
    <!--		更新者-->
    <!--		创建者-->
    <!--		派单时间-->

  </view>
</template>

<script>
export default {
  data() {
    const currentDate = this.getDate({
      format: true
    })
    return {
      date: currentDate,
      zyRepair: {
        // 报修ID
        repairId: '',
        // 报修编号
        repairNum: '',
        // 报修状态
        repairState: '',
        // 派单时间
        assignmentTime: '',
        // 接单时间
        receivingOrderTime: '',
        // 处理完成时间
        completeTime: '',
        // 取消时间
        cancelTime: '',
        // 期待上门时间
        doorTime: '2024-09-01',
        // 分派人Id
        assignmentId: '',
        // 处理人ID
        completeId: '',
        // 处理人电话
        completePhone: '',
        // 处理人姓名
        completeName: '',
        // 备注
        remark: '',
        // 创建者
        createBy: '',
        // 创建时间
        createTime: '',
        // 更新者
        updateBy: '',
        // 更新时间
        updateTime: '',
        // 创建人Id
        userId: '',
        // 删除状态
        delFlag: '',
        // 报修内容
        repairContent: '',
        // 小区Id
        communityId: '',
        // 详细地址
        address: '',
      },
      // 社区集合
      zyCommunityList:[1,2,3],
      //  社区集合索引
      zyCommunityIndex:0,
      // 选中的社区名
      selectedzyCommunityName:123
    }
  },
  methods: {
    bindDateChange: function (e) {
      this.zyRepair.doorTime = e.detail.value
    },
    onKeyInput: function(event) {
      this.zyRepair.repairContent = event.target.value
    },
    bindPickerChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.zyCommunityIndex = e.detail.value
      this.selectedzyCommunityName=this.zyCommunityList[this.zyCommunityIndex].communityName
      this.zyRepair.communityId=this.zyCommunityList[this.zyCommunityIndex].communityId
    },
    onAddressKeyInput: function(event) {
      this.zyRepair.address = event.target.value
    },
    // 获取时间工具类
    getDate(type) {
      const date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();

      if (type === 'start') {
        year = year - 60;
      } else if (type === 'end') {
        year = year + 2;
      }
      month = month > 9 ? month : '0' + month;
      day = day > 9 ? day : '0' + day;
      return `${year}-${month}-${day}`;
    },
    loadCommunityList() {
      uni.request({
        url: "communities",
        data: {},
        method: "GET",
        success: (res) => {
          this.zyCommunityList = res.data.data;
          console.log(this.zyCommunityList)
          this.zyCommunityIndex=0
          this.selectedzyCommunityName=this.zyCommunityList[this.zyCommunityIndex].communityName
          this.zyRepair.communityId=this.zyCommunityList[this.zyCommunityIndex].communityId
        },
      })
    },
    // 提交表单
    commit(){
      let username = uni.getStorageSync('username');
      this.zyRepair.createBy=username
      uni.request({
        url: "zyRepair",
        data: this.zyRepair,
        method: "POST",
        success: (res) => {
          console.log("添加成功")
          },
      })
    },
    changeLog(e) {
      this.zyRepair.doorTime=this.zyRepair.doorTime.replace(" ","T")
    },
  },
  computed: {
    // 计算起始时间
    startDate() {
      return this.getDate('start');
    },
    // 计算结束时间
    endDate() {
      return this.getDate('end');
    }
  },
  mounted() {
    this.loadCommunityList();
  }

}
</script>

<style>

</style>
